package androidx.test.espresso.action;

import android.net.Uri;
import android.text.Spanned;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;
import androidx.test.espresso.PerformException;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions;
import androidx.test.espresso.core.internal.deps.guava.collect.Lists;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.espresso.util.HumanReadables;
import java.util.ArrayList;
import java.util.Arrays;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

/* loaded from: classes.dex */
public final class OpenLinkAction implements ViewAction {
    private final Matcher<String> linkTextMatcher;
    private final Matcher<Uri> uriMatcher;

    public OpenLinkAction(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher) {
        this.linkTextMatcher = (Matcher) Preconditions.checkNotNull(linkTextMatcher);
        this.uriMatcher = (Matcher) Preconditions.checkNotNull(uriMatcher);
    }

    @Override // androidx.test.espresso.ViewAction
    public Matcher<View> getConstraints() {
        return Matchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(TextView.class), ViewMatchers.hasLinks());
    }

    @Override // androidx.test.espresso.ViewAction
    public String getDescription() {
        return String.format("open link with text %s and uri %s", this.linkTextMatcher, this.uriMatcher);
    }

    @Override // androidx.test.espresso.ViewAction
    public void perform(UiController uiController, View view) {
        TextView textView = (TextView) view;
        String charSequence = textView.getText().toString();
        URLSpan[] urls = textView.getUrls();
        Spanned spanned = (Spanned) textView.getText();
        ArrayList newArrayList = Lists.newArrayList();
        for (URLSpan uRLSpan : urls) {
            int spanStart = spanned.getSpanStart(uRLSpan);
            boolean z = spanStart != -1;
            String valueOf = String.valueOf(uRLSpan);
            StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 49);
            sb.append("Unable to get start of text associated with url: ");
            sb.append(valueOf);
            Preconditions.checkState(z, sb.toString());
            int spanEnd = spanned.getSpanEnd(uRLSpan);
            boolean z2 = spanEnd != -1;
            String valueOf2 = String.valueOf(uRLSpan);
            StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 47);
            sb2.append("Unable to get end of text associated with url: ");
            sb2.append(valueOf2);
            Preconditions.checkState(z2, sb2.toString());
            String substring = charSequence.substring(spanStart, spanEnd);
            newArrayList.add(substring);
            if (this.linkTextMatcher.matches(substring) && this.uriMatcher.matches(Uri.parse(uRLSpan.getURL()))) {
                uRLSpan.onClick(view);
                return;
            }
        }
        throw new PerformException.Builder().withActionDescription(getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new RuntimeException(String.format("Link with text '%s' and uri '%s' not found. List of links found in this view: %s\nList of uris: %s", this.linkTextMatcher, this.uriMatcher, newArrayList, Arrays.asList(urls)))).build();
    }
}
